/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.authorize.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.cshoo.tattoo.authorize.api.dto.AddGroupReq;
import org.cshoo.tattoo.authorize.api.dto.AddNewRoleReq;
import org.cshoo.tattoo.authorize.api.dto.AddNewUserReq;
import org.cshoo.tattoo.authorize.api.dto.UpdateGroupReq;
import org.cshoo.tattoo.authorize.app.GroupAppService;
import org.cshoo.tattoo.authorize.domain.po.Group;
import org.cshoo.tattoo.embed.infra.entity.Response;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
@Api(tags = "群组接口")
@Slf4j
@RestController
@RequestMapping(value = "/group")
public class GroupController {

    @Resource
    private GroupAppService groupAppService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加群组")
    public Response<?> addGroup(@RequestBody AddGroupReq addGroupReq) {
        groupAppService.addGroup(addGroupReq);
        return Response.builder().build();
    }

    @RequestMapping(value = "/addExistingUser", method = RequestMethod.POST)
    @ApiOperation("添加已经存在的用户")
    public Response<?> addExistingUser(@RequestParam Long groupId, @RequestParam Long userId) {
        groupAppService.addExistingUser(groupId, userId);
        return Response.builder().build();
    }

    @RequestMapping(value = "/addNewUser", method = RequestMethod.POST)
    @ApiOperation("添加新用户")
    public Response<?> addNewUser(@RequestBody AddNewUserReq addNewUserReq) {
        groupAppService.addNewUser(addNewUserReq);
        return Response.builder().build();
    }

    @RequestMapping(value = "/addNewRole", method = RequestMethod.POST)
    @ApiOperation("添加新角色")
    public Response<?> addNewRole(@RequestBody AddNewRoleReq addNewRoleReq) {
        log.info("addNewRoleReq:{}", addNewRoleReq);
        groupAppService.addNewRole(addNewRoleReq);
        return Response.builder().build();
    }

    @RequestMapping(value = "/assignExistingRole", method = RequestMethod.POST)
    @ApiOperation("分配已经存在的角色")
    public Response<?> assignExistingRole(@RequestParam Long groupId, @RequestParam Long roleId) {
        groupAppService.assignExistingRole(groupId, roleId);
        return Response.builder().build();
    }

    @RequestMapping(value = "/dismissRole", method = RequestMethod.DELETE)
    @ApiOperation("解除角色")
    public Response<?> dismissRole(@RequestParam Long groupId, @RequestParam Long roleId) {
        groupAppService.dismissRole(groupId, roleId);
        return Response.builder().build();
    }

    @RequestMapping(value = "/removeUser", method = RequestMethod.DELETE)
    @ApiOperation("删除用户")
    public Response<?> removeUser(@RequestParam Long groupId, @RequestParam Long userId) {
        groupAppService.removeUser(groupId, userId);
        return Response.builder().build();
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ApiOperation("修改群组信息")
    public Response<?> updateGroup(@RequestBody UpdateGroupReq updateGroupReq) {
        groupAppService.updateGroup(updateGroupReq);
        return Response.builder().build();
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    @ApiOperation("查询所有群组")
    public List<Group> findAllGroup() {
        return groupAppService.findAllGroup();
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ApiOperation("删除群组")
    public Response<?> deleteGroup(@PathVariable("id") Long id) {
        log.info("delete group id:{}", id);
        groupAppService.deleteGroup(id);
        return Response.builder().build();
    }

}